package com.liu.UDPSender;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * 接收端
 */
public class serverA {
    public static void main(String[] args) throws IOException {
        //创建端口
        DatagramSocket datagramSocket = new DatagramSocket(7777);
        //创建需要数据的容器
        byte[] buf = new byte[1024];
        //将接收的数据打包
        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
        //接收数据
        datagramSocket.receive(datagramPacket);

        //将实际接收到的数据长度
        int length = datagramPacket.getLength();
        //实际的数据
        byte[] data = datagramPacket.getData();
        //将数据传入S
        String s = new String(data, 0, length);
        //输出
        System.out.println(s);



        buf = "好的，明天见".getBytes();
        datagramPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.56.1"), 7778);
        datagramSocket.send(datagramPacket);
        //关闭端口
        datagramSocket.close();
    }
}
